﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using KaoQin.BLL;
using KaoQin.Common;
using KaoQin.Model;
using System.IO;
using System.Data;

namespace KaoQin.WPFApp.kaoQin
{
    /// <summary>
    /// KaoQinStatistics.xaml 的交互逻辑
    /// </summary>
    public partial class KaoQinStatistics : Window
    {
        public KaoQinStatistics()
        {
            InitializeComponent();
            //dpTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
            intYesr.Value = DateTime.Now.Year;
            intMonth.Value = DateTime.Now.Month;
            intYesr2.Value = DateTime.Now.Year;
            intMonth2.Value = DateTime.Now.Month;
            cbswipeState.ItemsSource = StaticDataHelper.GetSwipeState();
            btnExport2.IsEnabled = false;
        }

        private void txttodayNormal_Click(object sender, RoutedEventArgs e)
        {
            //ShowEmpKaoQinList(int.Parse(txttodayNormal.Content.ToString().Split('/')[0]), 0, Convert.ToDateTime(dpTime.Text), Convert.ToDateTime(dpTime.Text));
        }

        private void txttodayLate_Click(object sender, RoutedEventArgs e)
        {
            //ShowEmpKaoQinList(int.Parse(txttodayLate.Content.ToString()), 1, Convert.ToDateTime(dpTime.Text), Convert.ToDateTime(dpTime.Text));
        }

        private void ShowEmpKaoQinList(int recordCount, int state, DateTime beginTime, DateTime endTime)
        {
            kaoQin.EmpKaoQinList emplsit = new kaoQin.EmpKaoQinList(recordCount, state, beginTime, endTime);
            emplsit.ShowDialog();
        }

        private void txttodayNo_Click(object sender, RoutedEventArgs e)
        {
            //ShowNoList(int.Parse(txttodayNo.Content.ToString()), 0, Convert.ToDateTime(dpTime.Text), Convert.ToDateTime(dpTime.Text));
        }

        private void ShowNoList(int recordCount, int state, DateTime beginTime, DateTime endTime)
        {
            kaoQin.NoList nolist = new kaoQin.NoList(recordCount, state, beginTime, endTime);
            nolist.ShowDialog();
        }

        private void txtAfterNormal_Click(object sender, RoutedEventArgs e)
        {
            //ShowEmpKaoQinList(int.Parse(txtAfterEarly.Content.ToString()),2, Convert.ToDateTime(dpTime.Text), Convert.ToDateTime(dpTime.Text));
        }

        private void txtAfterEarly_Click(object sender, RoutedEventArgs e)
        {
            //ShowEmpKaoQinList(int.Parse(txtAfterEarly.Content.ToString()), 3, Convert.ToDateTime(dpTime.Text), Convert.ToDateTime(dpTime.Text));
        }

        private void txtAfterNo_Click(object sender, RoutedEventArgs e)
        {
            //ShowNoList(int.Parse(txtAfterNo.Content.ToString()), 1, Convert.ToDateTime(dpTime.Text), Convert.ToDateTime(dpTime.Text));
        }

        private void dpTime_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            //bindData(DateTime.Parse(dpTime.Text));
        }

        private void bindData(DateTime time)
        {
            try
            {
                SpecialCmd scmd = new SpecialCmd();
                //txttodayNormal.Content = scmd.GetNormal(time, time);
                //txttodayLate.Content = scmd.GetLate(time, time);
                //txttodayNo.Content = scmd.GetNo(time, time);
                //txtAfterEarly.Content = scmd.GetEndEarly(time, time);
                //txtAfterNo.Content = scmd.GetEndNo(time, time);
                //txtAfterNormal.Content = scmd.GetAfterNormal(time, time);
            }
            catch (Exception ex)
            {
                MessageBox.Show("绑定数据出错了:" + ex.Message);
            }
        }

        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            //bindData(DateTime.Parse(dpTime.Text));
        }

        private void DataPager_PageChanged(object sender, DataPager.PageChangedEventArgs e)
        {
            bindList();
        }

        private void btnLook_Click(object sender, RoutedEventArgs e)
        {
            string photoPath = (sender as Button).Tag.ToString();
            if (File.Exists(FileHelper.GetcurrPath() + "\\" + photoPath))
            {
                BigPhoto bigphotodlg = new BigPhoto();
                bigphotodlg.SetImg(photoPath);
                bigphotodlg.ShowDialog();
            }
            else
            {
                MessageBox.Show("照片不存在");

            }
        }

        private void bindList()
        {
            SpecialCmd bll = new SpecialCmd();
            try
            {
                DateTime bTime=Convert.ToDateTime(""+intYesr.Value+"-"+intMonth.Value);
                DateTime eTime=bTime.AddMonths(1);
                int recordCount = 0;
                lsv.ItemsSource = bll.GetDetialList(cbswipeState.SelectedValue.ToString(), bTime, eTime, txtUserName.Text.Trim(), pager.PageSize, pager.PageIndex, out recordCount).Tables[0].DefaultView;
                pager.TotalCount = recordCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("绑定数据出错了:" + ex.Message);
            }
        }
        private void bindDayList()
        {
            SpecialCmd bll = new SpecialCmd();
            try
            {
                DateTime bTime = Convert.ToDateTime("" + intYesr2.Value + "-" + intMonth2.Value);
                DateTime eTime = bTime.AddMonths(1);
                //int recordCount = 0;
                lsv2.ItemsSource = bll.GetdayDetialList(bTime, eTime, txtUserName2.Text.Trim()).Tables[0].DefaultView;
                //pager2.TotalCount = recordCount;
            }
            catch (Exception ex)
            {
                MessageBox.Show("绑定日明细数据出错:" + ex.Message);
            }
        }

        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            bindList();
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            SpecialCmd bll = new SpecialCmd();
            try
            {
                DateTime bTime=Convert.ToDateTime(""+intYesr.Value+"-"+intMonth.Value);
                DateTime eTime=bTime.AddMonths(1);
                btnExport.IsEnabled = false;
                DataSet ds = bll.ExportAll(cbswipeState.SelectedValue.ToString(), bTime, eTime, txtUserName.Text.Trim());
                if (ds.Tables[0] == null || ds.Tables[0].Rows.Count == 0)
                {
                    MessageBox.Show("没有数据");
                    btnExport.IsEnabled = true;
                    return;
                }

                //ExportFile.SaveAsExcelSetFile(ds);
                ExportFile.SaveAsExcelTableFile(ds.Tables[0]);

                btnExport.IsEnabled = true;
            }
            catch (Exception ex)
            {

                MessageBox.Show("导出出错：" + ex.Message);
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                MessageBoxResult result = MessageBox.Show("确定删除吗？", "确定", MessageBoxButton.YesNo);
                if (result == MessageBoxResult.Yes)
                {
                    string id = (sender as Button).Tag.ToString();
                    new BLL.SwipeCardRecord().Delete(id);
                    MessageBox.Show("删除成功");

                    bindList();
                }



            }
            catch (Exception ex)
            {
                MessageBox.Show("删除失败:" + ex.Message);
            }
        }

        private void btnGet2_Click(object sender, RoutedEventArgs e)
        {
            bindDayList();
            btnExport2.IsEnabled = true;
        }

        private void btnExport2_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                
                btnExport2.IsEnabled = false;
                DataTable tb = ((DataView)lsv2.ItemsSource).Table;
                if (tb == null || tb.Rows.Count == 0)
                {
                    MessageBox.Show("没有数据");
                    btnExport.IsEnabled = true;
                    return;
                }

                //ExportFile.SaveAsExcelSetFile(ds);
                ExportFile.SaveAsExcelTableFile2(tb);

                btnExport2.IsEnabled = true;
            }
            catch (Exception ex)
            {

                MessageBox.Show("导出出错：" + ex.Message);
            }
        }

        private void pager2_PageChanged(object sender, DataPager.PageChangedEventArgs e)
        {
            bindDayList();
        }

        
    }
}
